﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;
using log4net;
using System.Xml.XPath;
using System.Xml;
using System.Collections;

namespace Cn.Edwon.Web {
    public class ConfigurationSectionHandler : IConfigurationSectionHandler {
        public static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        #region IConfigurationSectionHandler 成员
        public object Create(object parent, object configContext, System.Xml.XmlNode section) {
            log.Debug("初始化节点："+section.Name);
            Hashtable configTable = new Hashtable();
            ParseAuthentication(section, ref configTable);
            ParseValidation(section, ref configTable);
            ParseCrud(section, ref configTable);
            return configTable;
        }
        #endregion

        /// <summary>
        /// 处理authentication节点
        /// </summary>
        /// <param name="section"></param>
        /// <param name="configTable"></param>
        private void ParseAuthentication(XmlNode section, ref Hashtable configTable) {
            try {
                XmlNode node = section.SelectSingleNode("authentication");
                if (node != null) {
                    String _AcTypeName;
                    String _AcConfigUri;
                    String _AcLoginUrl;
                    String _AcPowerLowUrl;
                    configTable.Add("AcEnable", true);
                    
                    _AcTypeName = node.Attributes["type"].Value;
                    configTable.Add("AcTypeName", _AcTypeName);
                    
                    if (node.Attributes["config-uri"] == null) {
                        _AcConfigUri = "";
                    } else {
                        _AcConfigUri = node.Attributes["config-uri"].Value;
                    }
                    configTable.Add("AcConfigUrl", _AcConfigUri);
                    
                    if (node.Attributes["login-url"] == null) {
                        _AcLoginUrl = "";
                    } else {
                        _AcLoginUrl = node.Attributes["login-url"].Value;
                    }
                    configTable.Add("AcLoginUrl", _AcLoginUrl);
                    
                    if (node.Attributes["power-low-url"] == null) {
                        _AcPowerLowUrl = "";
                    } else {
                        _AcPowerLowUrl = node.Attributes["power-low-url"].Value;
                    }
                    configTable.Add("AcPowerLowUrl", _AcPowerLowUrl);

                    String _RedirectAfterLogin;
                    if (node.Attributes["login-redirect"] == null) {
                        _RedirectAfterLogin = "";
                    } else {
                        _RedirectAfterLogin = node.Attributes["login-redirect"].Value;
                    }
                    configTable.Add("RedirectAfterLogin", _RedirectAfterLogin);

                    log.Debug("config:authentication:type = " + _AcTypeName);
                    log.Debug("config:authentication:url = " + _AcConfigUri);
                    log.Debug("config:authentication:login-url = " + _AcLoginUrl);
                    log.Debug("config:authentication:login-redirect = " + _RedirectAfterLogin);
                } else {
                    log.Debug("config:authentication disabled");
                    configTable.Add("AcEnable", false);
                    configTable.Add("AcTypeName", "");
                    configTable.Add("AcConfigUrl", "");
                    configTable.Add("AcLoginUrl", "");
                }
            } catch (Exception xex) {
                log.Error("加载节点jedwon/webSupport/authentication时出错", xex);
            }
        }
        /// <summary>
        /// 处理validation节点
        /// </summary>
        /// <param name="section"></param>
        /// <param name="configTable"></param>
        private void ParseValidation(XmlNode section, ref Hashtable configTable) {
            try {
                XmlNode valNode = section.SelectSingleNode("validation");
                if (valNode != null) {
                    configTable.Add("VdEnable", true);
                    //output-var
                    String _vdOutputVar;
                    if (valNode.Attributes["output-var"] != null) {
                        _vdOutputVar = valNode.Attributes["output-var"].Value;
                    } else {
                        _vdOutputVar = "";
                    }
                    configTable.Add("VdOutputVar", _vdOutputVar);
                    log.Debug("config:validation:output-var = " + _vdOutputVar);
                    //client-handler
                    String _vdClientHandler;
                    if (valNode.Attributes["client-handler"] != null) {
                        _vdClientHandler = valNode.Attributes["client-handler"].Value;
                    } else {
                        _vdClientHandler = "";
                    }
                    configTable.Add("VdClientHandler", _vdClientHandler);
                    log.Debug("config:validation:client-handler = " + _vdClientHandler);
                    //form-listener
                    String _vdFormListener;
                    if (valNode.Attributes["form-listener"] != null) {
                        _vdFormListener = valNode.Attributes["form-listener"].Value;
                    } else {
                        _vdFormListener = "";
                    }
                    configTable.Add("VdFormListener", _vdFormListener);
                    log.Debug("config:validation:form-listener = " + _vdFormListener);
                    //config-uri
                    String _vdConfigUri;
                    if (valNode.Attributes["config-uri"] != null) {
                        _vdConfigUri = valNode.Attributes["config-uri"].Value;
                    } else {
                        _vdConfigUri = "";
                    }
                    configTable.Add("VdConfigUri", _vdConfigUri);
                    log.Debug("config:validation:config-uri = " + _vdConfigUri);
                } else {
                    log.Debug("config:validation disabled");
                    configTable.Add("VdEnable", false);
                    configTable.Add("VdClientHandler", "");
                    configTable.Add("VdFormListener", "");
                    configTable.Add("VdConfigUri", "");
                }
            } catch (Exception xex) {
                log.Error("加载节点jedwon/webSupport/authentication时出错", xex);
            }
        }
        /// <summary>
        /// 处理Crud节点
        /// </summary>
        /// <param name="section"></param>
        /// <param name="configTable"></param>
        private void ParseCrud(XmlNode section, ref Hashtable configTable) {
            try {
                XmlNode crudNode = section.SelectSingleNode("crud");
                if (crudNode != null) {
                    configTable.Add("CrudEnable", true);
                    if (crudNode.Attributes["config-uri"] != null) {
                        configTable.Add("CrudConfigUri", crudNode.Attributes["config-uri"].Value);
                    } else {
                        configTable.Add("CrudConfigUri", "");
                    }
                } else {
                    configTable.Add("CrudEnable", false);
                    configTable.Add("CrudConfigUri", "");
                }
            } catch (Exception xex) {
                log.Error("加载节点jedwon/webSupport/crud时出错", xex);
            }
        }
    }
}
